package com.ruoyi.web.controller.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.UserModel;
import com.ruoyi.system.mapper.SysOrgPersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.fail;

/**
 * @Author hnb
 * @Date 2024/11/6
 * @Description 对接金蝶-云星空系统
 **/

@RestController
@RequestMapping("/kingdee")
public class KingdeeAcquisitionController {
    @Autowired
    private SysOrgPersonMapper sysOrgPersonMapper;
    /**
     * 获取员工
     * @return
     */
    @Log(title = "获取员工", businessType = BusinessType.OTHER)
    @GetMapping("/getEmployees")
    public AjaxResult getEmployees(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\n" +
                "    \"FormId\": \"BD_Empinfo\",\n" +
                "    \"FieldKeys\": \"FName,Fnumber,FBaseProperty3\",\n" +
                "    \"FilterString\": [],\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                userModel.setFBaseProperty3((String)list.get(2));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 获取部门
     * @return
     */
    @Log(title = "获取部门", businessType = BusinessType.OTHER)
    @GetMapping("/getDepartment")
    public AjaxResult getDepartment(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\n" +
                "    \"FormId\": \"BD_Department\",\n" +
                "    \"FieldKeys\": \"FName,Fnumber\",\n" +
                "    \"FilterString\": [],\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 获取客户
     * @return
     */
    @Log(title = "获取客户", businessType = BusinessType.OTHER)
    @GetMapping("/getCustomers")
    public AjaxResult getCustomers(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\n" +
                "    \"FormId\": \"BD_Customer\",\n" +
                "    \"FieldKeys\": \"FName,Fnumber\",\n" +
                "    \"FilterString\": \"FDocumentStatus='C'\",\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }


    /**
     * 获取供应商
     * @return
     */
    @Log(title = "获取供应商", businessType = BusinessType.OTHER)
    @GetMapping("/getSuppliers")
    public AjaxResult getSuppliers(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\n" +
                "    \"FormId\": \"BD_Supplier\",\n" +
                "    \"FieldKeys\": \"FName,Fnumber\",\n" +
                "    \"FilterString\": [],\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }



    /**
     * 获取物料
     * @return
     */
    @Log(title = "获取物料", businessType = BusinessType.OTHER)
    @GetMapping("/getMaterials")
    public AjaxResult getMaterials(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\"FormId\":\"BD_MATERIAL\",\"FieldKeys\":\"FName,FNumber,FSpecification,FBaseUnitId.FNumber as FUNumber,FUseOrgId.FName as FMatsterName\",\"FilterString\":\"FDocumentStatus='C'\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            String resultJson = String.valueOf(client.executeBillQuery(jsonData));
            System.out.println("接口返回结果: " + resultJson);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                userModel.setSpecification((String)list.get(2));
                userModel.setUnit((String)list.get(3));
                userModel.setShiyongzuzhi((String)list.get(4));
                resultList.add(userModel);
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }



    /**
     * 获取科目
     * @return
     */
    @Log(title = "获取科目", businessType = BusinessType.OTHER)
    @GetMapping("/getSubjects")
    public AjaxResult getSubjects(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\n" +
                "    \"FormId\": \"BD_Account\",\n" +
                "    \"FieldKeys\": \"FName,Fnumber\",\n" +
                "    \"FilterString\": [],\n" +
                "    \"OrderString\": \"\",\n" +
                "    \"TopRowCount\": 0,\n" +
                "    \"StartRow\": 0,\n" +
                "    \"Limit\": 2000,\n" +
                "    \"SubSystemId\": \"\"\n" +
                "}";
        String resultJson = null;
        List<Object> resultList = new ArrayList<>();
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            for (List<Object> list : lists) {
                UserModel userModel = new UserModel();
                userModel.setFName((String)list.get(0));
                userModel.setFNumber((String)list.get(1));
                resultList.add(userModel);
            }
            resultJson = String.valueOf(lists);
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 采购申请单
     * @return
     */
    @Log(title = "采购申请单", businessType = BusinessType.OTHER)
    @PostMapping("/purchaseRequisition")
    public AjaxResult purchaseRequisition(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "PUR_Requisition";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                    //调用接口
                    String resultJson1 = client.submit(formId,jsonData1);
                    //对返回结果进行解析和校验
                    RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                    if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                    } else {
                        System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                        return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                    }
                    if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                        String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                                 repoRet.getResult().getId()
                               + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                        //调用接口
                        String resultJson2 = client.audit(formId, jsonData2);
                        //对返回结果进行解析和校验
                        RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                            System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                        } else {
                            System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                        }
                    }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 采购订单
     * @return
     */
    @Log(title = "采购订单", businessType = BusinessType.OTHER)
    @PostMapping("/purchaseOrder")
    public AjaxResult purchaseOrder(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "PUR_PurchaseOrder";
            //调用接口
            String resultJson = client.save(formId,jsonData);

            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);

                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);

                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 费用报销单
     * @return
     */
    @Log(title = "费用报销单", businessType = BusinessType.OTHER)
    @PostMapping("/expenseReimbursement")
    public AjaxResult expenseReimbursement(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        ObjectMapper objectMapper = new ObjectMapper();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "ER_ExpReimbursement";
            //调用接口
            String resultJson = client.save(formId,jsonData);

            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);

                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);

                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 付款申请单
     * @return
     */
    @Log(title = "付款申请单", businessType = BusinessType.OTHER)
    @PostMapping("/paymentApplication")
    public AjaxResult paymentApplication(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "CN_PAYAPPLY";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                            repoRet.getResult().getId()
                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
                    //调用接口
                    String resultJson2 = client.audit(formId, jsonData2);
                    //对返回结果进行解析和校验
                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
                    } else {
                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
                    }
                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 付款单
     * @return
     */
    //FKDLX01_SYS 单据类型 采购订单付款单
    //FKDLX04_SYS  费用报销付款单
    @Log(title = "付款单", businessType = BusinessType.OTHER)
    @PostMapping("/paymentOrder")
    public AjaxResult paymentOrder(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
        try{
            //业务对象标识
            String formId = "AP_PAYBILL";
            //调用接口
            String resultJson = client.save(formId,jsonData);
            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口保存返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口保存返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                return AjaxResult.error(repoRet.getResult().getResponseStatus().getErrors().get(0).getMessage());
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                //调用接口
                String resultJson1 = client.submit(formId,jsonData1);
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口提交返回结果: %s%n", gson.toJson(repoRet.getResult()));
                } else {
                    System.out.printf("接口提交返回结果: %s%n",gson.toJson(repoRet.getResult().getResponseStatus()));
                    return AjaxResult.error(repoRet1.getResult().getResponseStatus().getErrors().get(0).getMessage());
                }
//                if(repoRet1.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet1.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
//                    String jsonData2 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
//                            repoRet.getResult().getId()
//                            + "\",\"InterationFlags\":\"\",\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"true\",\"IgnoreInterationFlag\":\"\",\"UseBatControlTimes\":\"false\"}";
//                    //调用接口
//                    String resultJson2 = client.audit(formId, jsonData2);
//                    //对返回结果进行解析和校验
//                    RepoRet repoRet2 = gson.fromJson(resultJson2, RepoRet.class);
//                    if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
//                        System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet2.getResult()));
//                    } else {
//                        System.out.println("接口返回结果: " + gson.toJson(repoRet2.getResult().getResponseStatus()));
//                    }
//                }
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 销售订单
     * @return
     */
    @Log(title = "销售订单", businessType = BusinessType.OTHER)
    @PostMapping("/salesOrder")
    public AjaxResult salesOrder(){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = "{\"FormId\":\"SAL_SaleOrder\",\"FieldKeys\":\"FID,FBillNo\"," +
                "\"FilterString\":[]," +
                "\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        String resultJson = "";
        try {
            //调用接口
            resultJson = String.valueOf(client.executeBillQuery(jsonData));
            System.out.println("接口返回结果: " + resultJson);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(resultJson);
    }

    /**
     * 发货通知
     * @return
     */
    @Log(title = "发货通知", businessType = BusinessType.OTHER)
    @PostMapping("/deliveryNotice")
    public AjaxResult deliveryNotice(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
//        String jsonData = "{\"FormId\":\"SAL_DELIVERYNOTICE\",\"FieldKeys\":\"FID,FBillNo\"," +
//                "\"FilterString\":[{\"FieldName\":\"FOrderNo\",\"Compare\":\"67\",\"Value\":\"XSDD000479\",\"Left\":\"\",\"Right\":\"\",\"Logic\":\"0\"}]," +
//                "\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        String jsonData = "{\"FormId\":\"SAL_DELIVERYNOTICE\",\"FieldKeys\":\"FBillNo\"," +
                "\"FilterString\": \"FOrderNo='" +
                map.get("FOrderNo")
                +"'\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
        String resultJson = "";
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery(jsonData);
            resultJson = lists.stream().map(innerList -> innerList.stream().map(l -> String.valueOf(l)).collect(Collectors.joining(","))).distinct().collect(Collectors.joining(","));
//          resultJson = String.valueOf(lists.get(0).get(0));
            System.out.println("接口返回结果: " + String.valueOf(lists));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("操作成功",resultJson);
    }

    /**
     * 凭证提交
     * @return
     */
    @Log(title = "凭证提交", businessType = BusinessType.OTHER)
    @PostMapping("/voucher")
    public AjaxResult voucher(@RequestBody Map<String,Object> map){
        K3CloudApi client = new K3CloudApi();
        //请求参数，要求为json字符串
        String jsonData = JSONObject.toJSONString(map);
//        String jsonData = "{\"NeedUpDateFields\":[],\"NeedReturnFields\":[],\"IsDeleteEntry\":\"true\",\"SubSystemId\":\"\",\"IsVerifyBaseDataField\":\"false\",\"IsEntryBatchFill\":\"true\",\"ValidateFlag\":\"true\",\"NumberSearch\":\"true\",\"IsAutoAdjustField\":\"false\",\"InterationFlags\":\"\",\"IgnoreInterationFlag\":\"\",\"IsControlPrecision\":\"false\",\"ValidateRepeatJson\":\"false\",\"Model\":{\"FVOUCHERID\":0,\"FAccountBookID\":{\"FNumber\":\"\"},\"FDate\":\"1900-01-01\",\"FBUSDATE\":\"1900-01-01\",\"FYEAR\":0,\"FPERIOD\":0,\"FVOUCHERGROUPID\":{\"FNumber\":\"\"},\"FVOUCHERGROUPNO\":\"\",\"FATTACHMENTS\":0,\"FISADJUSTVOUCHER\":\"false\",\"FSourceBillKey\":{\"FNumber\":\"\"},\"FIMPORTVERSION\":\"\",\"FDocumentStatus\":\"\",\"FEntity\":[{\"FEntryID\":0,\"FEXPLANATION\":\"\",\"FACCOUNTID\":{\"FNumber\":\"\"},\"FDetailID\":{\"FDETAILID__FFLEX9\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX4\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX5\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX6\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX8\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX10\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX11\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX15\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX12\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX13\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX14\":{\"FNumber\":\"\"},\"FDETAILID__FFLEX16\":{\"FNumber\":\"\"}},\"FCURRENCYID\":{\"FNumber\":\"\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"\"},\"FEXCHANGERATE\":0,\"FUnitId\":{\"FNUMBER\":\"\"},\"FPrice\":0,\"FQty\":0,\"FAMOUNTFOR\":0,\"FDEBIT\":0,\"FCREDIT\":0,\"FSettleTypeID\":{\"FNumber\":\"\"},\"FSETTLENO\":\"\",\"FBUSNO\":\"\",\"FEXPORTENTRYID\":0}]}}";
        try{
            //业务对象标识
            String formId = "GL_VOUCHER";
            //调用接口
            String resultJson = client.save(formId,jsonData);

            //用于记录结果
            Gson gson = new Gson();
            //对返回结果进行解析和校验
            RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
            if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
                System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet.getResult()));
            } else {
                System.out.printf("接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            }
            if(repoRet.getResult().getResponseStatus().isIsSuccess()&& StringUtils.isNotEmpty(repoRet.getResult().getResponseStatus().getSuccessEntitys().get(0).getId())) {
                //请求参数，要求为json字符串
                String jsonData1 = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" +
                        repoRet.getResult().getId()+
                        "\",\"SelectedPostId\":0,\"UseOrgId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                String resultJson1 = client.submit(formId,jsonData1);
                //用于记录结果
                Gson gson1 = new Gson();
                //对返回结果进行解析和校验
                RepoRet repoRet1 = gson1.fromJson(resultJson1, RepoRet.class);
                if (repoRet1.getResult().getResponseStatus().isIsSuccess()) {
                    System.out.printf("接口返回结果: %s%n", gson.toJson(repoRet1.getResult()));
                } else {
                    fail("接口返回结果: " + gson.toJson(repoRet1.getResult().getResponseStatus()));
                }
            }
        } catch (Exception e) {
            System.out.printf(e.getMessage());
        }
        return AjaxResult.success();
    }

}
